#include "list.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int head_create(NODE_T **head)
{
    NODE_T *hp = NULL;

    hp = malloc(sizeof(NODE_T));
    if (NULL == hp)
    {
        perror("malloc_fail.....");
        goto err0;
    }
    memset(hp, 0, sizeof(NODE_T));
    hp->next = NULL;

    *head = hp;

    return 0;
err0:
    return -1;
}

int node_append(NODE_T *head, int num)
{
    NODE_T *tmp = head;
    NODE_T *new_node = NULL;

    // 容错判断
    if (head == NULL)
    {
        perror("head = NULL??");
        goto err0;
    }

    // 创建节点空间
    new_node = malloc(sizeof(NODE_T));
    if (new_node == NULL)
    {
        perror("malloc fail...");
        goto err0;
    }
    memset(new_node, 0, sizeof(NODE_T));

    // 添加节点数据
    new_node->num = num;
    new_node->next = NULL;

    // 查找链表尾部
    while (tmp->next != NULL)
    {
        tmp = tmp->next;
    }

    // 插入数据
    tmp->next = new_node;

    return 0;

err0:
    return -1;
}

int list_traverse(NODE_T *head)
{
    NODE_T *tmp = head->next;

    // 遍历输出
    while (tmp != NULL)
    {
        printf("%d\n", tmp->num);
        tmp = tmp->next;
    }

    return 0;
}
